package com.example.demo.aqs;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wangadmin
 */
public class ReentrantLockTest {

    static Lock lock = new ReentrantLock();


    public static void main(String[] args) {

        Condition newCondition=lock.newCondition();
        newCondition.awaitUninterruptibly();
        for (int i = 0; i < 5; i++) {
            new Thread(new ThreadDemo(i)).start();
        }

    }

    static class ThreadDemo implements Runnable {
        Integer id;

        public ThreadDemo(Integer id) {
            this.id = id;
        }

        @Override
        public void run() {
            try {
                TimeUnit.MILLISECONDS.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < 2; i++) {
                lock.tryLock();
                try {
                    System.out.println("获得锁的线程：" + id + ",:" + i);
                } catch (Exception e) {

                } finally {
                    lock.unlock();
                }
            }
        }
    }
}
